package com.thomas.framework.controller.admin;

import com.thomas.framework.core.annotation.Function;
import com.thomas.framework.core.annotation.FunctionMenu;
import com.thomas.framework.entity.SysDictionaryGroup;

import com.thomas.framework.service.ISysDictionaryGroupService;
import com.thomas.framework.utils.CommonUtil;
import com.thomas.framework.utils.JsonUtil;
import com.thomas.framework.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * SysDictionaryGroup Controller
 * Created by taohj on 2014/5/23.
 */
@FunctionMenu("SysDictionaryGroupManagement")
@Controller
@RequestMapping("admin/sys/dictionary/group/")
public class SysDictionaryGroupController {




    private ISysDictionaryGroupService service;


    @Autowired
    public void setService(ISysDictionaryGroupService service) {
        this.service = service;
    }




    @Function("SysDictionaryGroupCreateView")
    @RequestMapping("createView")
    public  String createView(){
        return  "admin/sys/dictionary/group/create";
    }



    @Function("SysDictionaryGroupCreate")
    @ResponseBody
    @RequestMapping("create")
    public  String create(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "description",required = false) String description
    ){
        PageResult pageResult=new PageResult();
        SysDictionaryGroup group=new SysDictionaryGroup();
        group.setName(name);
        group.setDescription(description);
        pageResult.setSuccess(service.create(group));
        return pageResult.toString();
    }



    @Function("SysDictionaryGroupRemove")
    @ResponseBody
    @RequestMapping("remove")
    public  String remove(@RequestParam("id") String id){
        PageResult pageResult=new PageResult();
        SysDictionaryGroup group=new SysDictionaryGroup();
        group.setId(id);
        pageResult.setSuccess(service.remove(group));
        return  pageResult.toString();
    }


    @Function("SysDictionaryGroupModifyView")
    @RequestMapping("modifyView")
    public  String  modifyView(HttpServletRequest request,
                               @RequestParam("id") String id){
        SysDictionaryGroup group=service.getById(new SysDictionaryGroup(id));
        request.setAttribute("group",group);
        return "admin/sys/dictionary/group/modify";
    }

    @Function("SysDictionaryGroupModify")
    @ResponseBody
    @RequestMapping("modify")
    public  String modify(
            @RequestParam(value = "id",required = false) String id,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "description",required = false) String description
    ){
        PageResult pageResult=new PageResult();
        SysDictionaryGroup group=new SysDictionaryGroup();
        group.setId(id);
        group.setName(name);
        group.setDescription(description);
        pageResult.setSuccess(service.modify(group));
        return  pageResult.toString();
    }




    @Function("SysDictionaryGroupManagement")
    @RequestMapping("management")
    public  String listView(){
        return  "admin/sys/dictionary/group/management";
    }



    @Function("SysDictionaryGroupList")
    @ResponseBody
    @RequestMapping("list")
    public String list(
            HttpServletRequest request,
            @RequestParam(value="id",required = false) String id,
            @RequestParam(value="name",required = false) String name
    ) {
        PageResult<SysDictionaryGroup> pageResult = new PageResult<SysDictionaryGroup>();
        pageResult.setT(new SysDictionaryGroup());
        pageResult.getT().setId(id);
        pageResult.getT().setName(name);
        CommonUtil.initPageResult(pageResult, request);
        pageResult=service.getByPage(pageResult);
        return JsonUtil.toString(pageResult);
    }

}
